<?php
// 连接数据库
require '../script/mysqlconfig.php';
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
    die("连接数据库失败: " . $conn->connect_error);
}

$csvFile = 'hosts.csv';

// 打开CSV文件
if (($handle = fopen($csvFile, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $values = [];
        $columns = [];
        for ($c=0; $c < $num; $c++) {
            $columns['`name`'] = "`" . $c . "`"; // 使用列名替换索引，如果你的表列名不是数字，请替换为实际的列名，例如：`id`, `name`, `email`
            $values[] = "'" . $data[$c] . "'"; // 转义特殊字符，防止SQL注入
        }
        $sql = "INSERT INTO `brand` (" . implode(",", $columns) . ") VALUES (" . implode(",", $values) . ")"; // 使用实际的表名和列名替换`your_table_name`和列索引
        if ($conn->query($sql) === TRUE) {
            echo "新记录插入成功";
        } else {
            echo "错误: " . $sql . "<br>" . $conn->error;
        }
        $conn->close();
    }
    fclose($handle);
} else {
    echo "无法打开文件";
}
